$(document).ready(function(){
var service_case_count = 0;
var default_service_number = $('#default_service').text();



//====================================================================================================================================
//  ADDS CSS FORMATTING

    var stringForStyles = '<style type="text/css">'+
     '* {   font-family: sans-serif;}'+

     '.attending_name_header{  font-size: 28px;  font-weight: bold;  margin-top: 11px;    }'+

     'body > form { display:none; }'+
     'table, th, td{ border: 1px solid black; font-size: 12px;}'+
     'td {   text-align: center;  }'+
     'table{ border-collapse: collapse; }'+
     'table th { background-color: #A4A4A4; font-size: 12px; }'+
     'tbody { border: 2px solid #1C1C1C; }'+
     'thead { border: 2px solid #1C1C1C; }'+

     'th:nth-child(1) { border-right-color: #848484; } '+
     'th:nth-child(2) { border-left-color: #848484; border-right-color: #848484; } '+
     'th:nth-child(3) { border-left-color: #848484; border-right-color: #848484; }'+
     'th:nth-child(4) { border-left-color: #848484; border-right-color: #848484; }'+
     'th:nth-child(5) { border-left-color: #848484; border-right-color: #848484; }'+
     'th:nth-child(6) { border-left-color: #848484; border-right-color: #848484; }'+
     'th:nth-child(7) { border-left-color: #848484; }'+

     'td:nth-child(1) { border-right-color: #A4A4A4; } '+
     'td:nth-child(2) { border-left-color: #A4A4A4; border-right-color: #A4A4A4; } '+
     'td:nth-child(3) { border-left-color: #A4A4A4; border-right-color: #A4A4A4; }'+
     'td:nth-child(4) { border-left-color: #A4A4A4; border-right-color: #A4A4A4; }'+
     'td:nth-child(5) { border-left-color: #A4A4A4; border-right-color: #A4A4A4; }'+
     'td:nth-child(6) { border-left-color: #A4A4A4; border-right-color: #A4A4A4; }'+
     'td:nth-child(7) { border-left-color: #A4A4A4; }'+

     '.column_one {     width: 3%;  border-right: 0; }'+
     '.column_two {     width: 3%;  border-left: 0; }'+
     '.column_three {   width: 25%; }'+
     '.column_four {     width: 10%; }'+
     '.column_five {     width: 2%; }'+
     '.column_six {     width: 15%; }'+
     '.column_seven {     width: 25%; }'+
     '.time{     font-weight:bold; font-size: 15px; }'+
     '.location_room{    font-size: 15px; font-weight:bold;}'+
     '.location{    font-size: 11px; }'+
     '.procedure{   font-size: 12px; }'+
     '.stay_type{   font-size: 10px; color:grey;}'+
     '.patient_name{    font-size: 15px;  font-weight:bold; }'+
     '.patient_MR{     font-size: 12px;  }'+
     '.patient_DOB{     font-size: 10px;  }'+
     '.patient_age{     font-size: 10px;  }'+
     '.comments{     font-size: 12px;  }'+
     '.hidden{     display:none;  }'+
     '.shown{     display:inline-block;  }'+

     'table tbody tr:nth-child(even) {     background-color: #F2F2F2;  }'+
     'table tbody tr:nth-child(odd) {     background-color: #FFFFFF;  }'+
	 'table tbody tr:hover {        background-color:#E0F8EC;  }'+
	 
	 '#date_div{  font-size: 30px; text-align: center; background-color: #F2F2F2;  border: 1px solid #BDBDBD;  border-bottom: 0px solid transparent; position: relative;  display: block;  padding: 6px;  width:850px;  border-top-left-radius: 10px;  border-top-right-radius: 10px;}' +
	 '#filter_area{  background-color: #F2F2F2;  border: 1px solid #BDBDBD;  position: relative;  display: inline-block;  padding: 6px;  width:auto; border-bottom-left-radius: 10px;  border-bottom-right-radius: 10px; }'+

     '.toggle_buttons{  width: 124px;  font-size: 18px;  color: black;  line-height: 18px;  text-align: center;  background-color: #E6E6E6;   border: 1px solid #A4A4A4; border-radius: 4px;  cursor: pointer;  display: inline-block;  float: left;   margin: 4px 4px 4px 0px;  padding: 4px 0 4px 0;  position: absolute;  }'+

     '.toggle_buttons:hover{  color:#0404B4;  background-color:#CED8F6;  }'+
     '.toggle_buttons:active{  background-color:#A9BCF5;  }'+

     '#select_all{  right: 140px; bottom: 5px;}'+
     '#select_none{right: 5px; bottom: 5px;}'+
     '#clear_float{  clear: both;  }'+

     '.button_container{   width: 200px;   background-color: #E6E6E6;   border: 1px solid #A4A4A4;   border-radius: 7px;   cursor: pointer;  padding-bottom: 7px;  margin: 4px;  display: inline-block; vertical-align:top; }'+

     '.button_header{  padding-top: 5px; height: 25px;  margin-bottom: -8px; padding-bottom:8px; line-height: 25px;   font-size: 20px;  font-weight: bold;   text-align: center;   cursor: pointer;  color: black; }'+

     '.button_header:hover{  color:#0404B4;  background-color: #CED8F6;  }'+
     '.button_header:active{  background-color:#A9BCF5;  }'+
     '.divider {     height: 0px;  width: auto;  border-top: 1px solid grey;  margin: 7px 5px 7px 5px;  }'+

     '.checkbox {  width: 17px;  height: 17px;  line-height: 17px;  margin: 0 0 1px 5px;   cursor: pointer;   display: inline-block;  position: relative;  vertical-align: middle;  }'+
     '.checkbox.checked {  background-image: url("https://entresources2.googlecode.com/svn/checkbox_checked_green.png");  background-size: 14px 14px;  background-repeat: no-repeat;  background-position: 50% 22%;  }'+
     '.checkbox.unchecked {  background-image: url("https://entresources2.googlecode.com/svn/checkbox_unchecked_gray2.png");   background-size: 14px 14px;  background-repeat: no-repeat;  background-position: 50% 22%;  }'+

     '.menu_button{  width: auto;   height: 20px;   line-height: 20px;  color: grey;  }'+

     '.menu_button:hover{  color:#0404B4;  background-color: #CED8F6;  }'+
     '.menu_button:active{  background-color:#A9BCF5;  }'+
     '.menu_button.button_active{  color: black;  }'+
     '.menu_button.button_inactive{  color: grey;  }'+

     '.button_attending_name{  height: 18px;   line-height: 18px;   font-size: 16px;   margin-left: 8px;    cursor: pointer;    display: inline-block;  }'+
     '.case_count {   font-size: 16px;  height: 20px;   line-height: 20px;  float: right;    margin: 0 12px 0 0;    display: inline-block;  }'+
 //    '.filtered_tables{  width:100%;  }'+
     '.cancelled{  background-image: url("https://entresources2.googlecode.com/svn/Cancelled.gif");  background-size: 80px 20px;  }'+

	 '.task_item {    font-size: 8px;  color:grey; BORDER-BOTTOM: grey 1px solid;  } ' +
     '.task_item_divider  { BORDER-BOTTOM: grey 2px solid;  } ' +
	 '.task_item_last {    font-size: 8px;   color:grey; } ' +
	 
     'a.backToTop:link, a.backToTop:visited {  padding:0 10px 0 10px; font-size: 16px; color:white !important; background-color:#151515; display:block; text-decoration:none; width 15px; height: 40px; line-height: 40px; position:fixed; bottom:10px; right:20px; z-index:1666 !important; opacity:0.4; border-radius:5px;  } '+
     'a.backToTop:hover {background-color:#08088A;}'+

	 '@media print  { .noPrint  { display: none  !important; } }' +	 
	 
     '</style>';
    $('head').append(stringForStyles);
//====================================================================================================================================



//====================================================================================================================================
//  ADDS THE HTML FOR THE FILTER BUTTONS AT THE TOP OF THE PAGE
$('body').append(   
       '<a name="backToTop" id="backToTop" class="noPrint"></a>' + 
       '<div id="date_div" class="noPrint"></div>' +  //<div id="date_text"><div id="date_DOW"></div></div>
       '<div id="filter_area" class="noPrint">' +

       '<div id="select_all" class="toggle_buttons">Select All</div>' +
       '<div id="select_none" class="toggle_buttons">Select None</div>' +

       '<div id="clear_float"></div>' +
       
       '<div class="button_container general">' +
       '<div class="button_header general">General</div>'+
       '<div class="divider"></div>' + 
       '<div id="Chandra__R__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Chandra, R</span> <div class="case_count">X</div> </div>' +
       '<div id="Clymer__M__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Clymer, M</span> <div class="case_count">X</div> </div>' +
       '<div id="Francis__D__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Francis, D</span> <div class="case_count">X</div> </div>' +
       '<div id="Garrett__C__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Garrett, C</span> <div class="case_count">X</div> </div>' +
       '<div id="Gelbard__A__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Gelbard, A</span> <div class="case_count">X</div> </div>' +
       '<div id="Labadie__R__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Labadie, R</span> <div class="case_count">X</div> </div>' +
       '<div id="Ossoff__R__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Ossoff, R</span> <div class="case_count">X</div> </div>' +
       '<div id="Turner__J__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Turner, J</span> <div class="case_count">X</div> </div>' +
       '<div id="Ries__W__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Ries, W</span> <div class="case_count">X</div> </div>' +
       '<div id="Russell__P__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Russell, P</span> <div class="case_count">X</div> </div>' +
       '<div id="Seibert__J__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Seibert, J</span> <div class="case_count">X</div> </div>' +
       '<div id="Stephan__S__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Stephan, S</span> <div class="case_count">X</div> </div>' +
       '<div id="Vinson__K__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Vinson, K</span> <div class="case_count">X</div> </div>' +
		'<div id="Wood__M__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Wood, M</span> <div class="case_count">X</div> </div>' +
       
	   '<div class="divider"></div>' + 
       '<div id="Chambless__L__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Chambless, L</span> <div class="case_count">X</div> </div>' +
       '<div id="Thompson__R__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Thompson, R</span> <div class="case_count">X</div> </div>' +
       '<div id="Weaver__K__button" class="menu_button general button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Weaver, K</span> <div class="case_count">X</div> </div>' +

       '</div>' +
       
       
       '<div class="button_container headandneck">' +
       '<div class="button_header headandneck">Head & Neck</div>'+
       '<div class="divider"></div>' + 
       '<div id="Groom__K__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Groom, K</span> <div class="case_count">X</div> </div>' +
       '<div id="Heaphy__J__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Heaphy, J</span> <div class="case_count">X</div> </div>' +
       '<div id="Johnston__B__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Johnston, B</span> <div class="case_count">X</div> </div>' +
       '<div id="Mannion__K__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Mannion, K</span> <div class="case_count">X</div> </div>' +
       '<div id="Netterville__J__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Netterville, J</span> <div class="case_count">X</div> </div>' +
       '<div id="Rohde__S__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Rohde, S</span> <div class="case_count">X</div> </div>' +
       '<div id="Sinard__R__button" class="menu_button headandneck button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Sinard, R</span> <div class="case_count">X</div> </div>' +
       '</div>' +
       
       '<div class="button_container otology">' +
       '<div class="button_header otology">Otology</div>'+
       '<div class="divider"></div>' + 
       '<div id="Bennett__M__button" class="menu_button otology button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Bennett, M</span> <div class="case_count">X</div> </div>' +
       '<div id="Haynes__D__button" class="menu_button otology button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Haynes, D</span> <div class="case_count">X</div> </div>' +
       '<div id="Rivas_Campo__A__button" class="menu_button otology button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Rivas Campo, A</span> <div class="case_count">X</div> </div>' +
       '<div id="Wanna__G__button" class="menu_button otology button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Wanna, G</span> <div class="case_count">X</div> </div>' +
       '</div>'+

       '<div class="button_container pediatrics">' +
       '<div class="button_header pediatrics">Pediatrics</div>'+
       '<div class="divider"></div>' + 
       '<div id="Chinnadurai__S__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Chinnadurai, S</span> <div class="case_count">X</div> </div>' +
       '<div id="Goudy__S__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Goudy, S</span> <div class="case_count">X</div> </div>' +
       '<div id="Eavey__R__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Eavey, R</span> <div class="case_count">X</div> </div>' +
       '<div id="Penn__Jr__E__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Penn, Jr, E</span> <div class="case_count">X</div> </div>'+
       '<div id="Penn_Jr__E__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Penn Jr, E</span> <div class="case_count">X</div> </div>'+
       '<div id="Virgin__Jr__F__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Virgin, Jr, F</span> <div class="case_count">X</div> </div>'+
       '<div id="Virgin_Jr__F__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Virgin Jr, F</span> <div class="case_count">X</div> </div>'+
       '<div id="Werkhaven__J__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Werkhaven, J</span> <div class="case_count">X</div> </div>'+
       '<div id="Wootten__C__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Wootten, C</span> <div class="case_count">X</div> </div>'+

       '<div class="divider"></div>' + 
       '<div id="Acra__S__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Acra, S</span> <div class="case_count">X</div> </div>'+
       '<div id="Austin__E__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Austin, E</span> <div class="case_count">X</div> </div>'+
       '<div id="Brown__R__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Brown, R</span> <div class="case_count">X</div> </div>'+
       '<div id="Fazili__M__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Fazili, M</span> <div class="case_count">X</div> </div>'+
       '<div id="Wellons__J__button" class="menu_button pediatrics button_active"> <div class="checkbox checked"></div> <span class="button_attending_name">Wellons, J</span> <div class="case_count">X</div> </div>'+ //John "Jay" Wellons, III, MD, MSPH

       '</div>'+
       
       '<div id="clear_float"></div>' +
       '</div>' 
       );
//====================================================================================================================================
   var raw_date_string = $('body > form > div > table > tbody > tr td > center:contains("Preliminary OR Schedule for ")').text();   
   var space_after_DOW = raw_date_string.indexOf(" ","Preliminary OR Schedule for ".length)
   var DOW_text_short = raw_date_string.substring("Preliminary OR Schedule for ".length,space_after_DOW);
   var date_text = raw_date_string.substring(space_after_DOW+1,space_after_DOW+6);
   var date_month_num = raw_date_string.substring(space_after_DOW+1,space_after_DOW+3);
   var date_month_text = raw_date_string.substring(space_after_DOW+4,space_after_DOW+6);
   var DOW_text_long; 
   switch (DOW_text_short){
    case "Mon":
       DOW_text_long = "Monday";  break;
    case "Tue":
       DOW_text_long = "Tuesday";  break;
    case "Wed":
       DOW_text_long = "Wednesday";  break;
    case "Thu":
       DOW_text_long = "Thursday";  break;
    case "Fri":
       DOW_text_long = "Friday";  break;
    case "Sat":
       DOW_text_long = "Saturday";  break;
    case "Sun":
       DOW_text_long = "Sunday";  break;
   };
   var date_month_long;
   switch (date_month_num){
    case "01":
       date_month_long = "Jan";  break;
    case "02":
       date_month_long = "Feb";  break;
    case "03":
       date_month_long = "March";  break;
    case "04":
       date_month_long = "April";  break;
    case "05":
       date_month_long = "May";  break;
    case "06":
       date_month_long = "June";  break;
    case "07":
       date_month_long = "July";  break;
    case "08":
       date_month_long = "Aug";  break;
    case "09":
       date_month_long = "Sept";  break;
    case "10":
       date_month_long = "Oct";  break;
    case "11":
       date_month_long = "Nov";  break;
    case "12":
       date_month_long = "Dec";  break;
   };
   $('#date_div').text(DOW_text_long +', ' + date_month_long + ' ' + date_month_text );
   //$('#date_DOW').text(DOW_text_long);
//====================================================================================================================================
// PULLS DATE
       
//====================================================================================================================================


       
//====================================================================================================================================
// ADDS EVENT LISTENERS TO THE FILTER BUTTONS

	var elements_to_add_listeners_to = document.getElementsByClassName("menu_button");
	for (var i = 0; i < elements_to_add_listeners_to.length; i++){
		elements_to_add_listeners_to[i].addEventListener("click", mouse_CLICK_button, false);
	};
   
	var elements_to_add_listeners_to = document.getElementsByClassName("button_header");
	for (var i = 0; i < elements_to_add_listeners_to.length; i++){
		elements_to_add_listeners_to[i].addEventListener("click", mouse_CLICK_header, false);
	};

	var elements_to_add_listeners_to = document.getElementsByClassName("toggle_buttons");
	for (var i = 0; i < elements_to_add_listeners_to.length; i++){
		elements_to_add_listeners_to[i].addEventListener("click", mouse_CLICK_toggle_button, false);
	};
//====================================================================================================================================



//====================================================================================================================================
// HIDES/SHOWS THE TABLE FOR THE BUTTON PRESSED
	function mouse_CLICK_button (){
		if ($(this).hasClass('button_active')) {
			remove_this($(this));
		} else {
			add_this($(this));
		}
		add_case_count_and_gray_out_attendings_without_any_cases();
	};
//====================================================================================================================================



//====================================================================================================================================
// TOGGLE THE 'SELECT ALL' VS 'SHOW NONE'
   function mouse_CLICK_toggle_button()
   {
    if(this.id =="select_none")
       {
       $('.menu_button').each(function(){
        remove_this($(this))
       });
       } else {
       $('.menu_button').each(function(){
        add_this($(this))
       });
       };
    add_case_count_and_gray_out_attendings_without_any_cases();
   }
//====================================================================================================================================


//====================================================================================================================================
// TOGGLE THE CLICKED SERVICE TO 'SHOW ALL' VS 'SHOW NONE'
   function mouse_CLICK_header (){
   var clicked_category_text = $(this).text();
   toggle_category(clicked_category_text);
   };
//====================================================================================================================================


//====================================================================================================================================
// TOGGLE THE SERVICE TO 'SHOW ALL' VS 'SHOW NONE'
function toggle_category (clicked_category_text){
   var clicked_category_var;
   clicked_category_text = clicked_category_text.toLowerCase();
   switch (clicked_category_text){
   case 'pediatrics':
    clicked_category_var = "pediatrics";
    break;
   case 'general':
    clicked_category_var = "general";
    break;
   case 'otology':
    clicked_category_var = "otology";
    break;
   case 'head & neck':
    clicked_category_var = "headandneck";
    break;
   default:
    alert("default");
   };

   if ($('.menu_button.' + clicked_category_var + ' .checked').length == 0){
   $('.menu_button.' + clicked_category_var + '').each(function(){
    add_this($(this))
   });
   } else {
   $('.menu_button.' + clicked_category_var + '').each(function(){
    remove_this($(this))
   });   
   };
   add_case_count_and_gray_out_attendings_without_any_cases();
};
//====================================================================================================================================

//====================================================================================================================================
// MARKS AN ATTENDING AS CHECKED/ACTIVE AND THEN SHOWS HIS/HER FILTERED TABLE
	function add_this(obj){
		obj.addClass('button_active');
		obj.removeClass('button_inactive');
		obj.children('.checkbox').addClass('checked');
		obj.children('.checkbox').removeClass('unchecked');
		var _obj = obj;
		var table_name = obj.attr('id');
		table_name = table_name.replace("__button","");
		$('#' + table_name + '').css('display', 'table');
		$('#' + table_name + '_header').css('display', 'table');   
   };
//====================================================================================================================================


//====================================================================================================================================
// MARKS AN ATTENDING AS UNCHECKED/INACTIVE AND THEN HIDES HIS/HER FILTERED TABLE
	function remove_this(obj){
		obj.removeClass('button_active');
		obj.addClass('button_inactive');
		obj.children('.checkbox').removeClass('checked');
		obj.children('.checkbox').addClass('unchecked');
		var table_name = obj.attr('id');
		table_name = table_name.replace("__button","");
		$('#' + table_name + '').css('display', 'none');
		$('#' + table_name + '_header').css('display', 'none');
   };
//====================================================================================================================================


//====================================================================================================================================
// BUILDS THE FILTERED TABLES
function build_table(search_string, table_name){
   $('body').append(
		'<div class="attending_name_header" id="' + table_name +'_header">' + search_string + '</div> ' +
		'<table id="' + table_name + '" class="filtered_tables">' +
			'<colgroup>' +
				'<col class="column_one">' +
				'<col class="table_columns column_two">' +
				'<col class="table_columns column_three">' +
				'<col class="table_columns column_four">' +
				'<col class="table_columns column_five">' +
				'<col class="table_columns column_six">' +
				'<col class="table_columns column_seven">' +
			'</colgroup>' +
		'<thead> ' +
			'<tr>' +
				'<th>Time</th>' +
				'<th>Room</th>' +
				'<th>Procedure</th>' +
				'<th>Patient</th>' +
				'<th>Task</th>' +
				'<th>Comments</th>' +
				'<th>Notes</th>' +
			'</tr>' +
		'</thead>' +
		'<tbody></tbody>' 
	);
   
   jQuery('td:contains("Surg:' + search_string + '")').filter(function(){
   
   var number_of_decendants = $(this).find('*').length;

   if (number_of_decendants == 1) {
    service_case_count++;
//    console.log("service_case_count = "+service_case_count);
    var selected_table = $(this).parent().parent().text();
    var attending = $(this).text();
    var location = $(this).parent().prev().children(":first").text(); 
//    location = location.replace(/ RM /g,"<br>");
	var location_room = location.substr(location.indexOf("RM")+2, location.length);
	location = location.substr(0, location.indexOf("RM")-1);

//	var location_room = x;
	
    var time = $(this).parent().prev().children(":nth-child(2)").text();
	time = time.replace("-","-<br>");
	time = time.replace(/01/g,"1");
	time = time.replace(/02/g,"2");
	time = time.replace(/03/g,"3");
	time = time.replace(/04/g,"4");
	time = time.replace(/05/g,"5");
	time = time.replace(/06/g,"6");
	time = time.replace(/07/g,"7");
	time = time.replace(/08/g,"8");
	time = time.replace(/09/g,"9");
	
    var service = $(this).next().text();
    var procedure = $(this).parent().next().children().children(":first").text();
	var stay_type = $(this).parent().prev().children(":nth-child(5)").text();
	stay_type = stay_type.replace("S AMB STAY", "SAS");
	stay_type = stay_type.replace("AM ADMIT", "Inpatient");
	stay_type = stay_type.replace("INPATIENT", "Inpatient");
	stay_type = stay_type.replace("OBSERVE", "Obs");

	var number_of_brackets = (procedure.match(/\[/g) || []).length;
	var i = 0;
	for (i = 0; i < number_of_brackets; i++){
		start_position = procedure.search(/\[/);
		end_position = procedure.search(/\]/);
		string_to_remove = procedure.substr(start_position,end_position-start_position+1);
		procedure = procedure.replace(string_to_remove, "");
	};

    procedure = procedure.replace(/;;/g,";"); 
    procedure = procedure.replace(/;Left/g,"<br><br>LEFT"); 
    procedure = procedure.replace(/;Right/g,"<br><br>RIGHT"); 
    procedure = procedure.replace(/Left/g,"LEFT"); 
    procedure = procedure.replace(/Right/g,"RIGHT"); 
    procedure = procedure.replace(/;No Laterality /g,"<br><br>");
    procedure = procedure.replace(/No Laterality /g,"");
    procedure = procedure.replace(/\(Antrotomy\); Intranasal/g,"(Antrotomy), Intranasal"); 
	procedure = procedure.replace(/\);/g,")<br><br>");
    procedure = procedure.replace(/Or /g,"or ");
    procedure = procedure.replace(/And /g,"and ");
    procedure = procedure.replace(/Of /g,"of ");
    procedure = procedure.replace(/For /g,"for ");
    procedure = procedure.replace(/The /g,"the ");
    procedure = procedure.replace(/W\/Wo Bx/g,"");
    procedure = procedure.replace(/Laryngoscopy Direct; Diagnostic/g,"DL");
    procedure = procedure.replace(/Bronchoscopy; Bronchial or Endobronchial/g,"Bronchoscopy");
    procedure = procedure.replace(/Bilateral Submucous Resection Inferior Turbinate\(S\)/g,"BITSMR"); 
    procedure = procedure.replace(/W\/O/g,"w/o"); 
    procedure = procedure.replace(/W\/WO/g,"w/wo"); 
    procedure = procedure.replace(/W\/Wo/g,"w/wo"); 
    procedure = procedure.replace(/W\//g,"w/"); 
    procedure = procedure.replace(/\(S\)/g,"(s)"); 
    procedure = procedure.replace(/\(\d{5}\)/g,""); 
    procedure = procedure.replace(/ \(\d{5}\)/g,""); 
   procedure = procedure.replace(/\(\d{5}\S{1}\)/g,""); 
    procedure = procedure.replace(/ \(\d{5}\S{1}\)/g,""); 
//    procedure = procedure.replace(/\(......\)/g,""); 
//    procedure = procedure.replace(/ \(......\)/g,""); 
    procedure = procedure.replace(/ Eua/g," EUA"); 
    procedure = procedure.replace(/Egd/g,"EGD"); 
    procedure = procedure.replace(/Abr Testing/g,"ABR Testing"); 
    procedure = procedure.replace(/w\/wo Bal/g,"w/wo BAL"); 
    procedure = procedure.replace(/Csf/g,"CSF"); 
    procedure = procedure.replace(/Ocr|ocr/g,"OCR"); 
    procedure = procedure.replace(/Nims Monitor/g,"NIMS Monitor "); 
    procedure = procedure.replace(/Laryngoscopy Direct/g,"Direct Laryngoscopy"); 
    procedure = procedure.replace(/Laryngoscopy Flexible/g,"Flexible Laryngoscopy"); 
    procedure = procedure.replace(/; Under 11/g,", <11"); 
	procedure = procedure.replace(/\(Sep Proc\)/g,"(sep proc)"); 
	procedure = procedure.replace(/Stereotactic Computer-Assisted Procedure; Cranial, Extradural/g,"Stereotactic Computer-Assisted Procedure"); 
	procedure = procedure.replace(/Tep /g,"TEP "); 
	procedure = procedure.replace(/Pej /g,"PEJ "); 
	procedure = procedure.replace(/Peg /g,"PEG "); 
	
	 
	// THIS FINDS THE "W/" FOLLOWED BY A CHARACTER AND INSERTS A SPACE
	var number_of_withs = (procedure.match(/w\/\S/g) || []).length;
	var i = 0;
	for (i = 0; i < number_of_withs; i++){
		start_position = procedure.search(/w\/\S/g);
		procedure = procedure.substr(0,start_position+2) + " " + procedure.substr(start_position+2);
	};

	// THIS FINDS THE ";" WITH NO SPACES ON EITHER SIDE AND INSERTS A SPACE AFTER
	number_of_withs = (procedure.match(/\S;\S/g) || []).length;
	var i = 0;
	for (i = 0; i < number_of_withs; i++){
		start_position = procedure.search(/\S;\S/g);
		procedure = procedure.substr(0,start_position+2) + " " + procedure.substr(start_position+2);
	};

/*		procedure_search_results = procedure.search(/w\/\S/g); 
	if (procedure_search_results > -1) {
		procedure = procedure.substr(0,procedure_search_results+2) + " " + procedure.substr(procedure_search_results+2);
	}

	procedure_search_results = procedure.search(/\S;\S/g); 
	console.log(procedure_search_results);
	if (procedure_search_results > -1) {
		procedure = procedure.substr(0,procedure_search_results+2) + " " + procedure.substr(procedure_search_results+2);
	}
*/	
	var status = $(this).next().next().text();; 
    status = status.replace("Status: ","");
    var status_html = "";
    if(status.toLowerCase() == 'cancelled'){
       status_html = 'cancelled';
    }; 
    var patient_name = $(this).parent().prev().children(":nth-child(4)").text();
    var patient_MR = $(this).parent().prev().children(":nth-child(6)").text();
    patient_MR = patient_MR.replace("-","");
    var patient_DOB = $(this).parent().prev().children(":nth-child(7)").text();
    var patient_age = patient_DOB.substring(patient_DOB.indexOf("(")+1,patient_DOB.length-1);
    patient_age = patient_age.replace(" yrs", "yo");
    patient_age = patient_age.replace(" yr", "yo");
    patient_age = patient_age.replace(" mos", "mo");
    patient_DOB = patient_DOB.replace("DOB:", "");
    patient_DOB = patient_DOB.substring(1,patient_DOB.indexOf("("));
	
    var comments = $(this).parent().next().next().next().children(":first").text();
    comments = comments.replace("Case Comments:", "");
//    console.log("ATTENDING = " + $(this).text()); 
//    console.log("LOCATION = " + $(this).parent().prev().children(":first").text()); 
//    console.log("TIME = " + $(this).parent().prev().children(":nth-child(2)").text()); 
//    console.log("SERVICE = " + $(this).next().text()); 
//    console.log("PROCEDURE = " + $(this).parent().next().children().children(":first").text()); 
//    console.log("STATUS = " + status); 
//    console.log("PATIENT = " + $(this).parent().prev().children(":nth-child(4)").text()); 
//    console.log("MR = " + $(this).parent().prev().children(":nth-child(6)").text()); 
//    console.log("DOB = " + $(this).parent().prev().children(":nth-child(7)").text()); 
//    console.log("COMMENTS = " + $(this).parent().next().next().next().children(":first").text()); 
       
	$('#' + table_name + '').find('tbody').append(
		'<tr class="'+ status_html + '">' + 
			'<td><span class="time">' + time + '</span></td>' + 
			'<td><span class="location_room">' + location_room + '</span><br><span class="location">' + location + '</span></td>' + 
			'<td><span class="procedure">' + procedure + '</span><br><br><span class="stay_type">(' + stay_type + ')</span></td>' + 
			'<td><span class="patient_name">' + patient_name + '</span><br><span class="patient_MR">' + patient_MR + '<br><span class="patient_age">' + patient_age + '</span><br><span class="patient_DOB">' + patient_DOB + '</span></td>' + 
			'<td><span class="tasks">' +
				'<div class="task_item">Consent</div>' +
				'<div class="task_item">H&P</div>' +
				'<div class="task_item task_item_divider">Mark</div>' +
				'<div class="task_item">IPON</div>' +
				'<div class="task_item task_item_divider">Remind</div>' +
				'<div class="task_item">Orders</div>' +
				'<div class="task_item">Rx</div>' +
				'<div class="task_item_last">F/up</div>' +
			'</span></td>' + 
			'<td><span class="comments">' + comments + '</span></td>' + 
			'<td><span class="notes"> </span></td>' + 
		'</tr>'
    );   
	};
	});
};
//====================================================================================================================================


//====================================================================================================================================
// CREATES ATTENDING NAME AND TABLE_NAME ARRAYS, THEN CALLS FUNCTIONS TO BUILD THE FILTERED TABLES
	var search_term_array = ['Bennett, M', 'Chinnadurai, S', 'Chandra, R', 'Clymer, M', 'Garrett, C', 'Gelbard, A', 'Goudy, S', 'Haynes, D', 'Francis, D', 'Johnston, B', 'Labadie, R', 'Mannion, K', 'Netterville, J', 'Ossoff, R', 'Penn, Jr, E', 'Penn Jr, E', 'Ries, W',  'Rivas Campo, A', 'Rohde, S', 'Russell, P', 'Seibert, J', 'Sinard, R', 'Stephan, S', 'Turner, J', 'Vinson, K',   'Virgin, Jr, F', 'Virgin Jr, F', 'Wanna, G', 'Werkhaven, J', 'Wootten, C', 'Weaver, K', 'Fazili, M', 'Acra, S', 'Brown, R', 'Austin, E', 'Wellons, J', 'Thompson, R', 'Chambless, L', 'Wood, M', 'Heaphy, J', 'Groom, K', 'Eavey, R'];

	var table_name_array = [];
	for (var i = 0; i < search_term_array.length; i++){
		var x = search_term_array[i];
		var table_name = x.replace(/ /g,"_");
		table_name = table_name.replace(/,/g,"_");
		table_name = table_name.replace(/'/g,"_");
		table_name_array.push(table_name);
		//   console.log(table_name);
	};

	for (var i = 0; i < search_term_array.length; i++){
		var curr_search_term = search_term_array[i];
		var curr_table_name = table_name_array[i];
		build_table(curr_search_term, curr_table_name);
	};
//====================================================================================================================================


//====================================================================================================================================
// ADDS THE CASE COUNT NEXT TO EACH ATTENDING'S NAME & GRAY'S OUT ATTENDINGS WITHOUT CASES
   function add_case_count_and_gray_out_attendings_without_any_cases(){
   for (var i = 0; i < table_name_array.length; i++){  
    
    var curr_table_name = table_name_array[i];
    
   //   console.log("table_name_array[i] =========================================== " + table_name_array[i] + " ("+table_name_array[i].length+")");
    var case_count = 0;
    if ( $('#' + curr_table_name + ' tbody tr').length !== 0 ) 
       {
       case_count = $('#' + curr_table_name + ' tbody tr').length;
       $('#' + curr_table_name + '__button .case_count').text(case_count);
       } else {
       $('#' + curr_table_name + '__button .case_count').text('--');
       };
    
    if (case_count == 0) {
       $('#' + curr_table_name + '__button').addClass('button_inactive');
       $('#' + curr_table_name + '__button').removeClass('button_active');
       $('#' + curr_table_name + '__button .checkbox').addClass('unchecked');
       $('#' + curr_table_name + '__button .checkbox').removeClass('checked');
       $('#' + curr_table_name + '').css('display', 'none');
       $('#' + curr_table_name + '_header').css('display', 'none');

    };
   //   console.log("curr_table_name = " + curr_table_name);
    
   };
   };
//====================================================================================================================================


//====================================================================================================================================
// SHOWS ONLY THE DEFAULT SERVICE, HIDES THE OTHERS
   switch(default_service_number){
   case "1":
    $('.menu_button.headandneck').each(function(){  remove_this($(this))  });   
    $('.menu_button.otology').each(function(){  remove_this($(this))  });   
    $('.menu_button.pediatrics').each(function(){  remove_this($(this))  });   
    break;
   case "2":
    $('.menu_button.general').each(function(){  remove_this($(this))  });   
    $('.menu_button.otology').each(function(){  remove_this($(this))  });   
    $('.menu_button.pediatrics').each(function(){  remove_this($(this))  });   
    break;
   case "3":
    $('.menu_button.general').each(function(){  remove_this($(this))  });   
    $('.menu_button.headandneck').each(function(){  remove_this($(this))  });   
    $('.menu_button.pediatrics').each(function(){  remove_this($(this))  });   
    break;
   case "4":
    $('.menu_button.general').each(function(){  remove_this($(this))  });   
    $('.menu_button.headandneck').each(function(){  remove_this($(this))  });   
    $('.menu_button.otology').each(function(){  remove_this($(this))  });   
    break;
   }
//====================================================================================================================================


add_case_count_and_gray_out_attendings_without_any_cases();

$('body').append('<a class="backToTop noPrint" href="#backToTop" rel="nofollow">Back to Top</a>');

$('#date_div').width($('#filter_area').width());

});